package com.ktgj.service.controller;

import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.web.page.TableDataInfo;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.service.domain.AirportService;
import com.ktgj.service.domain.ContractLinkAirportservice;
import com.ktgj.service.service.IAirportServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 机场服务Controller
 * 
 * @author ruoyi
 * @date 2021-09-03
 */
@ControllerAdvice
@RestController
@RequestMapping("/service")
public class AirportServiceController extends BaseController
{
    @Autowired
    private IAirportServiceService airportServiceService;

    /**
     * 查询机场服务列表
     */
    @PreAuthorize(hasPermi = "service:service:list")
    @GetMapping("/list")
    public TableDataInfo list(AirportService airportService)
    {
        startPage();
        List<AirportService> list = airportServiceService.selectAirportServiceList(airportService);
        return getDataTable(list);
    }

    /**
     * 导出机场服务列表
     */
    @PreAuthorize(hasPermi = "service:service:export")
    @Log(title = "机场服务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AirportService airportService) throws IOException
    {
        List<AirportService> list = airportServiceService.selectAirportServiceList(airportService);
        ExcelUtil<AirportService> util = new ExcelUtil<AirportService>(AirportService.class);
        util.exportExcel(response, list, "机场服务数据");
    }

    /**
     * 获取机场服务详细信息
     */
    @PreAuthorize(hasPermi = "service:service:query")
    @GetMapping(value = "/{airportServiceId}")
    public AjaxResult getInfo(@PathVariable("airportServiceId") Long airportServiceId)
    {
        return AjaxResult.success(airportServiceService.selectAirportServiceById(airportServiceId));
    }

    /**
     * 新增机场服务*/
    @PreAuthorize(hasPermi = "service:service:add")
    @Log(title = "机场服务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AirportService airportService)
    {
        return toAjax(airportServiceService.insertAirportService(airportService));
    }

    /**
     * 修改机场服务*/
    @PreAuthorize(hasPermi = "service:service:edit")
    @Log(title = "机场服务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AirportService airportService)
    {
        return toAjax(airportServiceService.updateAirportService(airportService));
    }

    /**
     * 删除机场服务*/
    @PreAuthorize(hasPermi = "service:service:remove")
    @Log(title = "机场服务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{airportServiceIds}")
    public AjaxResult remove(@PathVariable Long[] airportServiceIds)
    {
        return toAjax(airportServiceService.deleteAirportServiceByIds(airportServiceIds));
    }


    @Log(title = "机场服务合同价格维护", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addAirportServicePrice")
    public AjaxResult addAirportServicePrice(@RequestBody ContractLinkAirportservice contractLinkAirportservice) {
        //验证数据是否存在
        int result = airportServiceService.selectAirportPrice(contractLinkAirportservice.getContractId(),
                contractLinkAirportservice.getAirportServiceId());

        if (result > 0) {
             return AjaxResult.error("该服务项目价格已存在！");
        }
        //执行新增数据操作
        return toAjax(airportServiceService.addAirportServicePrice(contractLinkAirportservice));

    }

    /**
     * 获取机场服务列表，远程调用
     * @return
     */
    @GetMapping("/getAirportServiceList")
    public List<AirportService> getAirportServiceList(){
        List<AirportService> list = airportServiceService.getAirportServiceList();
        return list;
    }
}
